package com.shine.minepressure.ui.chart;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;

import com.shine.minepressure.R;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.BubbleChartData;
import lecho.lib.hellocharts.model.BubbleValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.BubbleChartView;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.LineChartView;
import lecho.lib.hellocharts.view.PieChartView;
import lecho.lib.hellocharts.view.PreviewLineChartView;

public class ViewPagerChartsFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public ViewPagerChartsFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static ViewPagerChartsFragment newInstance(int sectionNumber) {
        ViewPagerChartsFragment fragment = new ViewPagerChartsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_pager_charts, container, false);
        RelativeLayout layout = (RelativeLayout) rootView;
        int sectionNum = getArguments().getInt(ARG_SECTION_NUMBER);
        switch (sectionNum) {
            case 1:
                LineChartView lineChartView = new LineChartView(getActivity());
                lineChartView.setLineChartData(generateLineChartData());
                lineChartView.setZoomType(ZoomType.HORIZONTAL);

                /** Note: Chart is within ViewPager so enable container scroll mode. **/
                lineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

                layout.addView(lineChartView);
                break;
            case 2:
                ColumnChartView columnChartView = new ColumnChartView(getActivity());
                columnChartView.setColumnChartData(generateColumnChartData());
                columnChartView.setZoomType(ZoomType.HORIZONTAL);

                /** Note: Chart is within ViewPager so enable container scroll mode. **/
                columnChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

                layout.addView(columnChartView);
                break;
            case 3:
                BubbleChartView bubbleChartView = new BubbleChartView(getActivity());
                bubbleChartView.setBubbleChartData(generateBubbleChartData());
                bubbleChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);

                /** Note: Chart is within ViewPager so enable container scroll mode. **/
                bubbleChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

                layout.addView(bubbleChartView);
                break;
            case 4:
                PreviewLineChartView previewLineChartView = new PreviewLineChartView(getActivity());
                previewLineChartView.setLineChartData(generatePreviewLineChartData());

                /** Note: Chart is within ViewPager so enable container scroll mode. **/
                previewLineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

                Viewport tempViewport = new Viewport(previewLineChartView.getMaximumViewport());
                float dx = tempViewport.width() / 6;
                tempViewport.inset(dx, 0);
                previewLineChartView.setCurrentViewport(tempViewport);
                previewLineChartView.setZoomType(ZoomType.HORIZONTAL);

                layout.addView(previewLineChartView);
                break;
            case 5:
                PieChartView pieChartView = new PieChartView(getActivity());
                pieChartView.setPieChartData(generatePieChartData());

                /** Note: Chart is within ViewPager so enable container scroll mode. **/
                pieChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

                layout.addView(pieChartView);
                break;
        }

        return rootView;
    }

    private LineChartData generateLineChartData() {
        int numValues = 20;

        List<PointValue> values = new ArrayList<PointValue>();
        for (int i = 0; i < numValues; ++i) {
            values.add(new PointValue(i, (float) Math.random() * 100f));
        }

        Line line = new Line(values);
        line.setColor(ChartUtils.COLOR_GREEN);

        List<Line> lines = new ArrayList<Line>();
        lines.add(line);

        LineChartData data = new LineChartData(lines);
        data.setAxisXBottom(new Axis().setName("Axis X"));
        data.setAxisYLeft(new Axis().setName("Axis Y").setHasLines(true));
        return data;

    }

    private ColumnChartData generateColumnChartData() {
        int numSubcolumns = 1;
        int numColumns = 12;
        // Column can have many subcolumns, here by default I use 1 subcolumn in each of 8 columns.
        List<Column> columns = new ArrayList<Column>();
        List<SubcolumnValue> values;
        for (int i = 0; i < numColumns; ++i) {

            values = new ArrayList<SubcolumnValue>();
            for (int j = 0; j < numSubcolumns; ++j) {
                values.add(new SubcolumnValue((float) Math.random() * 50f + 5, ChartUtils.pickColor()));
            }

            columns.add(new Column(values));
        }

        ColumnChartData data = new ColumnChartData(columns);

        data.setAxisXBottom(new Axis().setName("Axis X"));
        data.setAxisYLeft(new Axis().setName("Axis Y").setHasLines(true));
        return data;

    }

    private BubbleChartData generateBubbleChartData() {
        int numBubbles = 10;

        List<BubbleValue> values = new ArrayList<BubbleValue>();
        for (int i = 0; i < numBubbles; ++i) {
            BubbleValue value = new BubbleValue(i, (float) Math.random() * 100, (float) Math.random() * 1000);
            value.setColor(ChartUtils.pickColor());
            values.add(value);
        }

        BubbleChartData data = new BubbleChartData(values);

        data.setAxisXBottom(new Axis().setName("Axis X"));
        data.setAxisYLeft(new Axis().setName("Axis Y").setHasLines(true));
        return data;
    }

    private LineChartData generatePreviewLineChartData() {
        int numValues = 50;

        List<PointValue> values = new ArrayList<PointValue>();
        for (int i = 0; i < numValues; ++i) {
            values.add(new PointValue(i, (float) Math.random() * 100f));
        }

        Line line = new Line(values);
        line.setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
        line.setHasPoints(false);// too many values so don't draw points.

        List<Line> lines = new ArrayList<Line>();
        lines.add(line);

        LineChartData data = new LineChartData(lines);
        data.setAxisXBottom(new Axis());
        data.setAxisYLeft(new Axis().setHasLines(true));

        return data;

    }

    private PieChartData generatePieChartData() {
        int numValues = 6;

        List<SliceValue> values = new ArrayList<SliceValue>();
        for (int i = 0; i < numValues; ++i) {
            values.add(new SliceValue((float) Math.random() * 30 + 15, ChartUtils.pickColor()));
        }

        PieChartData data = new PieChartData(values);
        return data;
    }

}